/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.app.module.partner.controller;

import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yanlight.app.module.partner.request.PartnOrderRequest;
import com.yanlight.app.module.partner.service.PartnOrderService;
import com.yanlight.support.http.Response;
import com.yanlight.util.DateTimeUtil;
import com.yanlight.util.ResponseUtil;
import com.yanlight.util.SessionUtil;

/**
 * 
 * @author haolw
 * @time 2018/07/23
 */
@Controller
@RequestMapping("/partn/order")
public class PartnOrderController {
	@Autowired
	private PartnOrderService partnOrderService;

	@PostMapping("/list")
	@ResponseBody
	public Response list(PartnOrderRequest request) {
		Integer userId = SessionUtil.getUserId();
		request.setUserId(userId);
		return ResponseUtil.success(partnOrderService.findPage(request));
	}

	@PostMapping("/export")
	public void export(HttpServletResponse httpResponse, PartnOrderRequest request) {
		Integer userId = SessionUtil.getUserId();
		request.setUserId(userId);
		OutputStream outputStream = null;
		try {
			outputStream = httpResponse.getOutputStream();
			String fileName = "order_".concat(DateTimeUtil.getCompactMillNowTime())+".xlsx";
			httpResponse.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));  
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		partnOrderService.export(outputStream, request);
		return;
	}

}
